package com.etoak.train.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.etoak.train.domain.Member;
import com.etoak.train.exception.BusinessException;
import com.etoak.train.exception.BusinessExceptionEnum;
import com.etoak.train.mapper.MemberMapper;
import com.etoak.train.req.MemberLoginReq;
import com.etoak.train.req.MemberRegisterReq;
import com.etoak.train.req.MemberSendCodeReq;
import com.etoak.train.resq.MemberLoginResp;
import com.etoak.train.service.MemberService;
import com.etoak.train.util.JwtUtil;
import com.etoak.train.util.SnowUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



/**
 * @Author by 孙有泽
 * @Date 2025/5/9 22:32
 * @Description 会员模块的实现
 */
@Service
public class MemberServiceImpl implements MemberService {
    private static final Logger LOG = LoggerFactory.getLogger(MemberServiceImpl.class);

   @Autowired
   private MemberMapper memberMapper;

    @Override
    public int count() {
        return memberMapper.count();
    }
    /*
     * @author 孙有泽
     * @date 2025/5/10 9:59
     * @description 会员注册
     */
    @Override
    public long register(MemberRegisterReq registerReq) {
        String mobile = registerReq.getMobile();
        //1.查询该手机号是否注册
        List<Member> memberList=memberMapper.selectByMobile(mobile);
        if(CollUtil.isNotEmpty(memberList)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        //2.注册
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        return member.getId();
    }
    /*
     * @author 孙有泽
     * @date 2025/5/10 10:50
     * @description 发送短信功能
     */
    @Override
    public void sendCode(MemberSendCodeReq sendCodeReq) {
        String mobile = sendCodeReq.getMobile();
        //1.查询该手机号是否注册 没有注册插入数据库
        List<Member> memberList=memberMapper.selectByMobile(mobile);
        if(CollUtil.isEmpty(memberList)){
            LOG.info("手机号不存在，插入一条记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else{
            LOG.info("手机号存在");
        }
        //2.发送短信
        //String code = RandomUtil.randomString(4);
        String code="9999";
        LOG.info("生成短信验证码：{}", code);

        // 保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间
        LOG.info("保存短信记录表");

        // 对接短信通道，发送短信
        LOG.info("对接短信通道");


    }
    /*
     * @author 孙有泽
     * @date 2025/5/10 11:09
     * @description 会员登录
     */
    @Override
    public MemberLoginResp login(MemberLoginReq memberLoginReq) {
        String mobile = memberLoginReq.getMobile();
        String code = memberLoginReq.getCode();
        Member memberDB = selectByMobile(mobile);

        // 如果手机号不存在，则插入一条记录
        if (ObjectUtil.isNull(memberDB)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
        // 校验短信验证码
        if (!"9999".equals(code)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }
        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
        String token = JwtUtil.createToken(memberLoginResp.getId(), memberLoginResp.getMobile());
        memberLoginResp.setToken(token);
        return memberLoginResp;

    }

        private Member selectByMobile(String mobile){
            List<Member> members = memberMapper.selectByMobile(mobile);
            if (CollUtil.isEmpty(members)) {
                return null;
            } else {
                return members.get(0);
            }
        }

}
